package net.malkuth.test;

import android.content.Context;
import android.os.Message;
import android.widget.Toast;

import com.lidroid.xutils.exception.DbException;

import net.malkuth.dao.DataBase;
import net.malkuth.entity.Test;
import net.malkuth.model.NewsToModel;
import net.malkuth.net.HandlerHelp;
import net.malkuth.view.XListView;
import net.malkuth.view.XListView.OnXListViewListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class NewsListActivity extends BaseNewActivity implements
        OnXListViewListener {
    private XListView listview;
    private NewsToModel newsToModel;
    private int page = 1;
    private String result;

    @Override
    public void setupView() {
        // TODO Auto-generated method stub
        //		listview = (XListView) findViewById(R.id.list_view);
    }

    @Override
    public void setContent() {
        // TODO Auto-generated method stub
        this.setContentView(R.layout.activity_main);

    }

    @Override
    public void setModel() {
        // TODO Auto-generated method stub
        new NewsListHandler(context).execute();
    }

    public class NewsListHandler extends HandlerHelp {

        public NewsListHandler(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            newsToModel = new NewsToModel(context);
        }

        @Override
        public void updateUI() {
            // TODO Auto-generated method stub
            Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
            Test test = getListPerson(result);
            try {
                DataBase.dbUtils.save(test);
                System.out.println("保存成功");
                List<Test> list = DataBase.dbUtils.findAll(Test.class);
                System.out.println("数据长度：" + list.size());
            } catch (DbException e) {
                e.printStackTrace();
            }
        }


        @Override
        public void doTask(Message msg) throws Exception {
            // TODO Auto-generated method stub
            result = newsToModel.getNewsList(page + "");
        }

        @Override
        public void doTaskAsNoNetWork(Message msg) throws Exception {
            // TODO Auto-generated method stub

        }

    }

    @Override
    public boolean getIntentValue() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public void onRefresh() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLoadMore() {
        // TODO Auto-generated method stub

    }

    public static Test getListPerson(String jsonString) {
        Test test = null;
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            test = new Test();
            test.setStatus(jsonObject.getInt("status"));
            test.setInfo(jsonObject.getString("info"));
            test.setData(list(jsonObject.getJSONArray("data").toString()));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return test;
    }

    private static List<Test.DataBean> list(String json) {
        List<Test.DataBean> list = new ArrayList<Test.DataBean>();
        Test.DataBean test;

        try {
            JSONArray jsonArray = new JSONArray(json);
            for (int i = 0; i < json.length(); i++) {
                test = new Test.DataBean();
                test.setNews_id(jsonArray.getJSONObject(i).getString("news_id"));
                test.setNews_summary(jsonArray.getJSONObject(i).getString("news_summary"));
                test.setNews_title(jsonArray.getJSONObject(i).getString("news_title"));
                test.setPic_url(jsonArray.getJSONObject(i).getString("pic_url"));
                list.add(test);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

}
